package com.chd.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.chd.dto.Result;
import com.chd.entity.ShopType;
import com.chd.mapper.ShopTypeMapper;
import com.chd.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.chd.utils.RedisConstants.CACHE_SHOP_TYPE;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result queryTList() {
        String shopTypeJSON = stringRedisTemplate.opsForValue().get(CACHE_SHOP_TYPE);
        if (StrUtil.isNotBlank(shopTypeJSON)) {
            // 3. 存在, 直接返回
            List<ShopType> shoptype = JSONUtil.toList(shopTypeJSON, ShopType.class);
            return Result.ok(shoptype);
        }

        List<ShopType> typeList = query().orderByAsc("sort").list();

        stringRedisTemplate.opsForValue().set(CACHE_SHOP_TYPE, JSONUtil.toJsonStr(typeList));
        return Result.ok(typeList);
    }
}
